//
//  JZHPhotoCell.m
//  JZHTest
//
//  Created by yyt on 14-8-5.
//  Copyright (c) 2014年 yyt. All rights reserved.
//

#import "JZHPhotoCell.h"
#import "JZHPersonaldetailsViewController.h"

#import "JZHTheDiary.h"

@implementation JZHPhotoCell
{
    JZHTheDiary *m_diary;
    BOOL isMyDynamic;
}

@synthesize arrayData;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        isMyDynamic = NO;
        [self addCellSubView];
        // Initialization code
    }
    return self;
}

///添加cell上的控件
- (void)addCellSubView
{
    //日期
    UILabel *day = [[UILabel alloc]initWithFrame:CGRectMake(13, 13, 23, 23)];
    [day setBackgroundColor:[UIColor clearColor]];
    [day setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    [day setTag:9001];
    [self.contentView addSubview:day];
    
    //月份
    UILabel *month = [[UILabel alloc]initWithFrame:CGRectMake(38, 18, 40, 15)];
    [month setBackgroundColor:[UIColor clearColor]];
    [month setFont:[UIFont systemFontOfSize:12]];
    [month setTag:9002];
    [self.contentView addSubview:month];
}

///添加动态简介
- (void)addDynamicView:(JZHTheDiary *)diary andNum:(int)num
{
    m_diary = diary;
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(80, 13 + num * 85, 220, 80)];
    [view setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:view];
    
    NSArray *arrayPhoto = diary.images;
    
    //图片
    UIView *photoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    [view addSubview:photoView];
    
    if (arrayPhoto.count == 0)
    {
    }
    else if (arrayPhoto.count == 1)
    {
        UIImageView *image = [[UIImageView alloc]initWithFrame:photoView.frame];
        [ImageCache imageUrl:[arrayPhoto objectAtIndex:0] UiImageview:image];
        [photoView addSubview:image];
    }
    else if (arrayPhoto.count == 2)
    {
        for (int i = 0; i < arrayPhoto.count; i++)
        {
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake((i % 2) * 40.5, 0, 40.5, 80)];
            [ImageCache imageUrl:[arrayPhoto objectAtIndex:0] UiImageview:image];
            [photoView addSubview:image];
        }
    }
    else
    {
        for (int i = 0; i < (arrayPhoto.count > 4?4:arrayPhoto.count); i++)
        {
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake((i % 2) * 40.5,
                                                                              (i / 2) * 40.5,
                                                                              39.5, 39.5)];
            [ImageCache imageUrl:[arrayPhoto objectAtIndex:0] UiImageview:image];
            [photoView addSubview:image];
        }
    }
    
    UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageBig:)];
    [photoView setAccessibilityIdentifier:[NSString stringWithFormat:@"%d", num]];
    [photoView addGestureRecognizer:tapImage];
    
    //动态内容
    NSString *strContent = diary.content;
    CGSize size = [strContent sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(135, 56) lineBreakMode:NSLineBreakByWordWrapping];
    UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(85, 0, 135, size.height)];
    [content setBackgroundColor:[UIColor clearColor]];
    [content setTextAlignment:NSTextAlignmentLeft];
    [content setFont:[UIFont systemFontOfSize:13]];
    [content setText:strContent];
    [content setNumberOfLines:0];
    [view addSubview:content];
    
    if (arrayPhoto.count > 1)
    {
        //图片张数
        UILabel *photoNum = [[UILabel alloc]initWithFrame:CGRectMake(85, 66, 135, 14)];
        [photoNum setBackgroundColor:[UIColor clearColor]];
        [photoNum setFont:[UIFont systemFontOfSize:10]];
        [photoNum setText:[NSString stringWithFormat:@"共%d张",arrayPhoto.count]];
        [view addSubview:photoNum];
    }
}

///设置非第一行
- (CGFloat)setCellData:(NSDictionary *)dict
{
    NSString *strDate = [dict objectForKey:@"time"];
    arrayData = [NSMutableArray arrayWithArray:[dict objectForKey:@"data"]];
    
    float height = 13;
    
    //日
    UILabel *day = (UILabel *)[self.contentView viewWithTag:9001];
    [day setText:[strDate substringFromIndex:3]];
    
    //月
    UILabel *month = (UILabel *)[self.contentView viewWithTag:9002];
    [month setText:[self month:[strDate substringToIndex:2]]];
    
    //当是他人动态时，今天的动态的显示
    NSString *now = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSString *dayAndMonthNow = [now substringWithRange:NSMakeRange(5, 5)];
    if ([strDate isEqualToString:dayAndMonthNow])
    {
        //天
        [day setFrame:CGRectMake(day.frame.origin.x, day.frame.origin.y, 65, 25)];
        [day setFont:[UIFont systemFontOfSize:25]];
        [day setText:@"今天"];
        
        //月
        [month setHidden:YES];
    }
    
    //动态
    for (int i = 0; i < arrayData.count; i++)
    {
        JZHTheDiary *diary = [arrayData objectAtIndex:i];
        [self addDynamicView:diary andNum:i];
        height = height + 83;
    }
    height = height + 10;
    return height;
}

///设置第一行
- (CGFloat)setFirstCellData:(NSArray *)array
{
    float height = 13;
    isMyDynamic = YES;
    arrayData = [NSMutableArray arrayWithArray:array];
    
    //日
    UILabel *day = (UILabel *)[self.contentView viewWithTag:9001];
    [day setFrame:CGRectMake(day.frame.origin.x, day.frame.origin.y, 65, 25)];
    [day setFont:[UIFont systemFontOfSize:25]];
    [day setText:@"今天"];
    
    //月
    UILabel *month = (UILabel *)[self.contentView viewWithTag:9002];
    [month setHidden:YES];
    
    //动态
    UIButton *selectPhoto = [[UIButton alloc]initWithFrame:CGRectMake(80, 13, 80, 80)];
    [selectPhoto setBackgroundImage:[UIImage imageNamed:@"Screenshot2"] forState:UIControlStateNormal];
    [selectPhoto addTarget:self action:@selector(selectPhotoAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:selectPhoto];
    height = height + 83;
    
    for (int i = 1; i < array.count + 1; i ++)
    {
        JZHTheDiary *diary = [array objectAtIndex:i - 1];
        [self addDynamicView:diary andNum:i];
        height = height + 83;
    }
    height = height + 10;
    return height;
}

///相机和相册按钮执行的方法
- (void)selectPhotoAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(openCameraAndAlbum)])
    {
        [self.delegate openCameraAndAlbum];
    }
}

#pragma mark - 计算月份
- (NSString *)month:(NSString *)month
{
    if ([month isEqualToString:@"01"])
    {
        return @"一月";
    }
    else if ([month isEqualToString:@"02"])
    {
        return @"二月";
    }
    else if ([month isEqualToString:@"03"])
    {
        return @"三月";
    }
    else if ([month isEqualToString:@"04"])
    {
        return @"四月";
    }
    else if ([month isEqualToString:@"05"])
    {
        return @"五月";
    }
    else if ([month isEqualToString:@"06"])
    {
        return @"六月";
    }
    else if ([month isEqualToString:@"07"])
    {
        return @"七月";
    }
    else if ([month isEqualToString:@"08"])
    {
        return @"八月";
    }
    else if ([month isEqualToString:@"09"])
    {
        return @"九月";
    }
    else if ([month isEqualToString:@"10"])
    {
        return @"十月";
    }
    else if ([month isEqualToString:@"11"])
    {
        return @"十一月";
    }
    else
    {
        return @"十二月";
    }
}
- (void)awakeFromNib
{
    // Initialization code
}

- (void)imageBig:(UITapGestureRecognizer *)tapGestureRecognizer
{
    if ([self.delegate respondsToSelector:@selector(imageViewBig:andIndex:)])
    {
        int index = [tapGestureRecognizer.view.accessibilityIdentifier intValue];
        if (isMyDynamic)
        {
            index = index - 1;
        }
        JZHTheDiary *diary = [arrayData objectAtIndex:index];
        
        [self.delegate imageViewBig:diary.images andIndex:@"0"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
